import { defineStore } from "pinia";
import { AuthState } from "@/stores/interface";
import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList } from "@/utils";
import { isPC } from "@/utils/sys";

export const useAuthStore = defineStore({
  id: "geeker-auth",
  state: (): AuthState => ({
    // 按钮权限列表
    authButtonList: {},
    // 菜单权限列表
    authMenuList: [],
    // 当前页面的 router name，用来做按钮权限筛选
    routeName: ""
  }),
  getters: {
    // 按钮权限列表
    authButtonListGet: state => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: state => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: state => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.authMenuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
  },
  actions: {
    // Get AuthButtonList
    async getAuthButtonList() {
      const { data } = await getAuthButtonListApi();
      this.authButtonList = data;
    },

    // Get AuthMenuList
    getAuthMenuList() {
      const { data } = getAuthMenuListApi();
      console.log(data);
      let dbc = [
        {
          path: "/mobileIndex/index",
          name: "mobileIndex",
          component: "/mobileIndex/index",
          meta: {
            icon: "icon-laboratory",
            title: "首页",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: false,
            isKeepAlive: true
          }
        },
        {
          path: "/laboratory/index",
          name: "laboratory",
          component: isPC() ? "/laboratory/index" : "/laboratoryMobile/index",
          meta: {
            icon: "icon-laboratory",
            title: "实验室管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,

            isAffix: false,
            isKeepAlive: true
          }
        },

        {
          path: "/experimentalist/index",
          name: "experimentalist",
          component: isPC() ? "/experimentalist/index" : "/experimentalistMobile/index",
          meta: {
            icon: "icon-experimentalist",
            title: "实验员管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,
            isAffix: false,
            isKeepAlive: true
          }
        },
        {
          path: "/laboratoryAppointment/index",
          name: "laboratoryAppointment",
          component: isPC() ? "/laboratoryAppointment/index" : "/laboratoryMobile/index",
          meta: {
            icon: "icon-laboratoryAppointment",
            title: "预约管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,
            isAffix: false,
            isKeepAlive: true
          }
        },
        {
          path: "/goods/index",
          name: "goods",
          // component: "/goods/index",
          component: isPC() ? "/goods/index" : "/goodsMobile/index",
          meta: {
            icon: "icon-goods",
            title: "货品管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,
            isAffix: true,
            isKeepAlive: true
          }
        },

        {
          path: "/addGoodsMobile/index",
          name: "addGoodsMobile",
          // component: "/goods/index",
          component: "/addGoodsMobile/index",
          meta: {
            icon: "icon-goods",
            title: "添加货品",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/exportGoodsExcelMobile/index",
          name: "exportGoodsExcelMobile",
          // component: "/goods/index",
          component: "/exportGoodsExcelMobile/index",
          meta: {
            icon: "icon-goods",
            title: "导出货品",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/lendGoodsMobile/index",
          name: "lendGoodsMobile",
          // component: "/goods/index",
          component: "/lendGoodsMobile/index",
          meta: {
            icon: "icon-goods",
            title: "借出货品",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/returnGoodsMobile/index",
          name: "returnGoodsMobile",
          // component: "/goods/index",
          component: "/returnGoodsMobile/index",
          meta: {
            icon: "icon-goods",
            title: "归还货品",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/goodsMobile/index",
          name: "goodsMobile",
          component: "/goodsMobile/index",
          meta: {
            icon: "icon-goods",
            title: "货品管理",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/category/index",
          name: "category",
          component: isPC() ? "/category/index" : "/categoryMobile/index",
          meta: {
            icon: "icon-category",
            title: "类别管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,
            isAffix: false,
            isKeepAlive: true
          }
        },
        {
          path: "/measurement/index",
          name: "measurement",
          component: isPC() ? "/measurement/index" : "/measurementMobile/index",
          meta: {
            icon: "icon-metrics",
            title: "度量管理",
            isLink: "",
            isCustomIcon: true,
            isHide: false,
            isFull: isPC() ? false : true,
            isAffix: false,
            isKeepAlive: true
          }
        },
        {
          path: "/selectGood/index",
          name: "selectGood",
          // component: "/goods/index",
          component: "/selectGood/index",
          meta: {
            icon: "icon-goods",
            title: "选择货品",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        },
        {
          path: "/lendReturnLogMobile/index",
          name: "lendReturnLogMobile",
          // component: "/goods/index",
          component: "/lendReturnLogMobile/index",
          meta: {
            icon: "icon-goods",
            title: "借还记录",
            isLink: "",
            isCustomIcon: true,
            isHide: true,
            isFull: true,
            isAffix: true,
            isKeepAlive: true
          }
        }
      ];
      console.log(dbc);
      this.authMenuList = data;
    },
    // Set RouteName
    async setRouteName(name: string) {
      this.routeName = name;
    }
  }
});
